package ltd.dujiabao.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author : DuJiabao
 * @Project : mybatisplus
 * @Time : 2021/4/16 18:32
 * @Desc :
 */
@Slf4j
@Component
public class MyBatisPlusUpdateCreateTimeHandler implements MetaObjectHandler {
  @Override
  public void insertFill(MetaObject metaObject) {
    log.info("start insert fill");
    this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
    this.setFieldValByName("version", 0, metaObject);
    this.setFieldValByName("deleted", 0, metaObject);
  }

  @Override
  public void updateFill(MetaObject metaObject) {
    log.info("start update fill");
    this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
  }
}
